// pages/commodityDetails/commodityDetails.js
const app = getApp();
const WxParse = require('../../components/wxParse/wxParse.js');
let Api = require('../../api/api.js');
let config = require('../../config.js');

Page({
    /**
     * 页面的初始数据
     */
    data: {
        tipText:'',
        showToast:false,
        clickBtnType:'',
        showModal: false,
        showTabs: false,
        tabsData:[{
            text:'图文详情',
        }, {
            text: '铸造工艺',
        }, {
            text: '购买须知',
        },],
        activeTabsIndex:0,
        detailBanner:[],
        indicatorDots: false,
        autoplay: true,
        duration: 1000,
        interval:5000,
        circular:true,
        dotsIndex: 1,
        money: '3000',
        money_1: '00',
        originalPrice:'4000.00',
        commodityName:'', //商品名称
        sold: '1000',
        address: '浙江杭州',
        tabsOpacity:false, //tabs透明度  false 不透明  true  半透明
        scrollTop: 0,
        toView:'',
        detailPic_1:'',
        wpCover: '',//封面图
        nameplateListData: [],//铭牌列表
        activeNameplateList: 0,//当前选中铭牌
        purchaseQuantity: 1,
        wsid:'', //铭牌属性id
        isCollection:'false', //有没有被收藏
        wpViceName:'',//副标题
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let self = this;
        app.globalData.wpid = options.wpID; //全局储存商品属性标识ID 
        Api.get(app.globalData.mallDomain, {
            doAllProduct: 1,
            pagesize: 10,
            pageindex: 1,
            wpid: options.wpID,
            token: app.globalData.token,
        }).then(res => {
            console.log(res,'-----商品详情-----------')
            if(res.data.msg == 100){
                //重置标签
                let resetTag = unescape(res.data.list[0].wpDetail, 0); //图文详情
                let resetTag_1 = unescape(res.data.list[0].wpDetailGongYi, 0);//制造工艺
                let resetTag_2 = unescape(res.data.list[0].wpDetailXuZhi, 0);//购买须知
                WxParse.wxParse('resetTag', 'html', resetTag, self, 0);
                WxParse.wxParse('resetTag_1', 'html', resetTag_1, self, 0);
                WxParse.wxParse('resetTag_2', 'html', resetTag_2, self, 0);

                //设置详情页面的title
                wx.setNavigationBarTitle({
                    title: res.data.list[0].wpName,
                })
                //去除最后一个逗号
                res.data.list[0].wpImgs = (res.data.list[0].wpImgs.substring(res.data.list[0].wpImgs.length - 1) == ',') ? res.data.list[0].wpImgs.substring(0, res.data.list[0].wpImgs.length - 1) : res.data.list[0].wpImgs ;

                self.setData({
                    commodityName: res.data.list[0].wpName,
                    money: parseInt(res.data.list[0].wpPrice),
                    money_1: res.data.list[0].wpPrice.substring(res.data.list[0].wpPrice.length - 2, res.data.list[0].wpPrice.length),
                    originalPrice: res.data.list[0].wpOldPrice,
                    sold: res.data.list[0].wpPeos,
                    address: res.data.list[0].wpOrigin,
                    detailBanner: res.data.list[0].wpImgs.split(","),
                    wpCover: res.data.list[0].wpCover,
                    isCollection: res.data.list[0].isCollection,
                    wpViceName: res.data.list[0].wpViceName,
                })
                Api.get(app.globalData.mallDomain, { //2）获取商品属性
                    doGetProductAttribute: 1,
                    wpid: res.data.list[0].wpID,
                }).then(res => {
                    console.log(res,'获取商品属性')
                    if (res.data.onevalue != '') {
                        self.setData({
                            nameplateListData: res.data.onevalue.split(","), //字符串重组
                            nameplate: res.data.onevalue.split(",")[0], //默认选中第一个
                        })
                        Api.get(app.globalData.mallDomain, { //3）取指定属性商品的价格和库存 获取第一个铭牌的wsid
                            doProductAttributePrice: 1,
                            wpid: app.globalData.wpid,
                            onevalue: res.data.onevalue.split(",")[0],
                        }).then(ret => {
                            self.setData({
                                wsid: ret.data.wsid
                            })
                        })
                    }
                })
            }
            
        })
        
    },
    /**
     * 点击收藏
     */ 
    clickCollection: function () {
        let self = this;
        Api.get(app.globalData.mallDomain, {
            doAddOrDelCollection: 1,
            token: app.globalData.token,
            wpid: app.globalData.wpid,
        }).then(res => {
            console.log(res)
            if(res.data.ret == 1){ //取消收藏成功
                self.setData({
                    isCollection:'false'
                })
                self.setData({
                    tipText: '取消收藏成功！',
                    showToast: true
                })
            } else if (res.data.ret == 2) { //添加收藏成功
                self.setData({
                    isCollection: 'true'
                })
                self.setData({
                    tipText: '添加收藏成功！',
                    showToast: true
                })
            }
            setTimeout(function () {
                self.setData({
                    tipText: '',
                    showToast: false
                })
            }, 1000)
        })
    },

    /**
     * 点击客服按钮
     */
    clickKefu: function () {
        let self = this;
        console.log('点击客服按钮')
       
    },
    /**
     * 点击立即购买
     */
    clickBuyNow: function () {
        let self = this;
        self.setData({
            showModal: true,
            clickBtnType: 'confirmOrder', 
        })

    },
    /**
     * 点击加入购物车
     */
    clickShoppingCart: function () {
        let self = this;
        self.setData({
            clickBtnType: 'shoppingCart',
            showModal: true,
        })
        
    }, 
    /**
     *点击弹出框的确定按钮
     */
    toOrderPage: function () {
        let self = this;
        self.setData({
            showModal: false,
        })
        if (self.data.clickBtnType == 'confirmOrder') {//进入到确认订单页面
            wx.navigateTo({
                url: '../confirmOrder/confirmOrder?sendType=送自己&nameplateType=' + self.data.nameplate + '&purchaseQuantity=' + self.data.purchaseQuantity + '&wpid=' + app.globalData.wpid + '&enterType=首页进入&wpName=' + self.data.commodityName + '&wpPrice=' + self.data.money + '.' + self.data.money_1 + '&wpCover=' + self.data.wpCover + '&wsid=' + self.data.wsid + '&wpViceName=' + self.data.wpViceName
            })
        } else if (self.data.clickBtnType == 'shoppingCart'){ //添加购物车
            Api.get(app.globalData.mallDomain, {
                doProductJoinCar: 1,
                token: app.globalData.token,
                wpid: app.globalData.wpid,
                wsid: self.data.wsid,
                number: self.data.purchaseQuantity,
            }).then(res => {
                wx.showToast({
                    title:'添加成功',
                    success:function(ret){
                        console.log(ret)
                    },
                    fail:function(err){
                        console.log(ret)
                    }
                })
            })
        }
    },
    /**
     * 添加数量
     */
    addVal: function () {
        let self = this;
        self.setData({
            purchaseQuantity: self.data.purchaseQuantity + 1,
        })
    },
    /** 
     * 减少数量
    */
    reductionVal: function () {
        let self = this;
        if (self.data.purchaseQuantity != 1) {
            self.setData({
                purchaseQuantity: self.data.purchaseQuantity - 1,
            })
        }
    },
    /**
     * 点击铭牌列表
     */
    clickNameplateList: function (e) {
        let self = this;
        self.setData({
            nameplate: e.currentTarget.dataset.text,
            activeNameplateList: e.currentTarget.dataset.index,
        })
        Api.get(app.globalData.mallDomain, { //3）取指定属性商品的价格和库存 
            doProductAttributePrice: 1,
            wpid: app.globalData.wpid,
            onevalue: e.currentTarget.dataset.text,
        }).then(ret => {
            self.setData({
                wsid: ret.data.wsid
            })
        })
    },
    /**
     * 关闭弹出框
     */
    closeBg: function (e) {
        let self = this;
        self.setData({
            showModal: false,
        })
    },
    /**
     * 获取滚动详情
     */
    scroll: function(e) {
        let self = this;
        if (self.data.showTabs == false){
            if (e.detail.scrollTop > 200){
                self.setData({
                    showTabs:true,
                    tabsOpacity:false,
                })
            }
        } else if (self.data.showTabs == true){
            if (e.detail.scrollTop < 200 && e.detail.scrollTop > 10) {
                self.setData({
                    tabsOpacity: true,
                    showTabs: true,
                })
            }else  if (e.detail.scrollTop < 10) {
                self.setData({
                    showTabs: false,
                    tabsOpacity: false,
                })
            } else if (e.detail.scrollTop > 200) {
                self.setData({
                    tabsOpacity: false,
                })
            }
        }
    },
    /**
     * 点击tabs
     */
    clickTabs: function(e) {
        let self = this;
        self.setData({
            activeTabsIndex:e.currentTarget.dataset.index,
            clickTabs:true,
        })
        if (e.currentTarget.dataset.index == 0){
            self.setData({
                toView:'graphicDetail'
            })
        } else if (e.currentTarget.dataset.index == 1){
            self.setData({
                toView: 'castingProcess'
            })
        } else if (e.currentTarget.dataset.index == 2) {
            self.setData({
                toView: 'purchaseInformation'
            })
        }
    },
    /**
     * 监听页面滑动
     */
    listeningSwiper: function (e) {
        let self = this;
        self.setData({
            dotsIndex: e.detail.current + 1
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log(12)

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})